package com.controller;

import com.bean.*;
import com.mvc.ResponseBody;
import com.service.EDeliverService;
import com.util.EDeliveryUtil;
import com.util.JSONUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * @author LEIKE
 * @Description: 接受参数 & 与前台交互 - 快递员
 * @Date_Created_in: 2021/5/27 16:00
 * @Modified_By:
 * @Project: ExpressDeliveryE
 */
public class EDeliveryController {

    @ResponseBody("/EDelivery/console.do")
    public String console(HttpServletRequest req, HttpServletResponse resp){
        Map<String, Integer> data = EDeliverService.console();
        Message msg = new Message("控制台数据",0,data);
        if (data.size() <= 0 || data == null) {
            msg.setStatus(-1);
        }
        return JSONUtil.toJSON(msg);
    }


    @ResponseBody("/EDelivery/list.do")
    public String list(HttpServletRequest req, HttpServletResponse resp){
        String off = req.getParameter("offset");
        int offset = 0, pageNumber = 0;
        if(off == null ){
            offset = 1;
            pageNumber = 5;
        }else{
            offset = Integer.parseInt(off);
            pageNumber = Integer.parseInt(req.getParameter("pageNumber"));
        }
        //分页查询数据
        List<EDelivery> list1 = EDeliverService.findAll(true , offset , pageNumber);
        if (list1 != null){
            List<BootStrapTableED> list2 = EDeliveryUtil.setBootStrapTableED(list1);
            //集合封装
            Map<String, Integer> console = EDeliverService.console();
            int total = console.get("data1_Size");
            //（集合数据 ，总数量）
            ResultData<BootStrapTableED> data = new ResultData(list2,total);
            return JSONUtil.toJSON(data);
        }
        return null;
    }
    
    
    @ResponseBody("/EDelivery/insert.do")
    public String insert(HttpServletRequest req, HttpServletResponse resp){
    
        String name = req.getParameter("name");
        String phone = req.getParameter("phone");
        String idnumber = req.getParameter("idnumber");
        String password = req.getParameter("password");
        
        EDelivery e = new EDelivery(name, phone, idnumber, password);
        boolean flag = EDeliverService.insert(e);
        Message msg = new Message();
        if(flag){
            msg.setStatus(0);
            msg.setResult("录入成功！");
        }else{
            msg.setStatus(-1);
            msg.setResult("录入失败,检查单号是否重复！");
        }
        return JSONUtil.toJSON(msg);
    }
    
    @ResponseBody("/EDelivery/find.do")
    public String find(HttpServletRequest req, HttpServletResponse resp){
        String phone = req.getParameter("phone");
        // System.out.println("find.do: " + phone);
        List<EDelivery> e = EDeliverService.findByPhoneNumber(phone);
        Message msg ;
        if (e == null){
            msg = new Message("手机号不存在",-1,null);
        }else{
            msg = new Message(0,e.get(0));
//            System.out.println(JSONUtil.toJSON(msg));
        }
        return JSONUtil.toJSON(msg);
    }
    
    @ResponseBody("/EDelivery/update.do")
    public String update(HttpServletRequest req, HttpServletResponse resp){
        long phone = 0;
        String name = req.getParameter("name");
        String phoneNumber = req.getParameter("phone");
        String idnumber = req.getParameter("idnumber");
        String password = req.getParameter("password");
        String number = req.getParameter("number");

        EDelivery newE = new EDelivery(name, phoneNumber, idnumber, password);

        boolean update = EDeliverService.update(number, newE);
        Message msg;
        if (update){
            msg = new Message("修改成功",0,null);
        }else{
            msg = new Message("修改失败,检查手机号是否重复！",-1,null);
        }
        return JSONUtil.toJSON(msg);
    }
    
    
    @ResponseBody("/EDelivery/delete.do")
    public String delete(HttpServletRequest req, HttpServletResponse resp){
        String phone = req.getParameter("phone");
        // System.out.println("delete.do: " + phone);
        boolean delete = EDeliverService.delete(phone);
        Message msg;
        if (delete){
            msg = new Message("删除成功",0,null);
        }else{
            msg = new Message("删除失败",-1,null);
        }
        return JSONUtil.toJSON(msg);
    }
    
}
